import axios from "axios";
import store from "../store";
import { Message } from "element-ui";

const service = axios.create({
  baseURL: "http://172.82.4.121:8080",
  //baseURL: 'http://192.168.12.4:8080/springmvcdemo_war',
  timeout: 1000 * 100 // request timeout
});

// request interceptor
service.interceptors.request.use(
  config => {
    //console.log(store.getters.getToken)
    // if (store.getters.getToken) {
    //   //config.headers['Authentication'] = store.getters.getToken
    // }
    return config;
  },
  error => {
    // do something with request error
    Message({
      message: res.message || "error",
      type: "error",
      duration: 2 * 1000
    });
    return Promise.reject(error);
  }
);

// response interceptor
service.interceptors.response.use(
  response => {
    // const res = response.data
    if (response.status !== 200) {
      Message({
        message: res.message || "error",
        type: "error",
        duration: 2 * 1000
      });
      return Promise.reject(response.msg || "error");
    } else {
      return response.data;
    }
  },
  error => {
    Message({
      message: error.response.data.msg,
      type: "error",
      duration: 2 * 1000
    });
    return Promise.reject(error);
  }
);

export default service;
